﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Security;
using System.Runtime.InteropServices;
using Microsoft.Office.SecureStoreService.Server;

namespace Diplo.Twitter
{
    public static class Helper
    {
        /// <summary>
        /// Get string from secure string
        /// </summary>
        /// <param name="secStr"></param>
        /// <returns></returns>
        public static string GetStringFromSecureString(this SecureString secStr)
        {
            if (secStr == null)
            {
                return null;
            }

            IntPtr pPlainText = IntPtr.Zero;
            try
            {
                pPlainText = Marshal.SecureStringToBSTR(secStr);
                return Marshal.PtrToStringBSTR(pPlainText);
            }
            finally
            {
                if (pPlainText != IntPtr.Zero)
                {
                    Marshal.FreeBSTR(pPlainText);
                }
            }
        }

        /// <summary>
        /// Make the string secure
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static SecureString MakeSecureString(this string str)
        {
            if (str == null)
            {
                return null;
            }
            SecureString str2 = new SecureString();
            char[] chArray = str.ToCharArray();
            for (int i = 0; i < chArray.Length; i++)
            {
                str2.AppendChar(chArray[i]);
                chArray[i] = '0';
            }
            return str2;
        }

        /// <summary>
        /// Get the secure store proxy
        /// </summary>
        /// <returns></returns>
        public static SecureStoreServiceApplicationProxy GetSecureStoreProxy()
        {
            SPServiceContext context = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);
            SecureStoreServiceApplicationProxy sssProxy = context.GetDefaultProxy(typeof(SecureStoreServiceApplicationProxy)) as SecureStoreServiceApplicationProxy;
            return sssProxy;
        }

        /// <summary>
        /// Get the central admin site
        /// </summary>
        /// <returns></returns>
        public static SPSite GetCentralAdminSite()
        {
            SPAdministrationWebApplication adminWebApp = SPAdministrationWebApplication.Local;
            if (adminWebApp == null)
            {
                throw new InvalidProgramException("Unable to get the admin web app");
            }

            SPSite adminSite = null;
            Uri adminSiteUri = adminWebApp.GetResponseUri(SPUrlZone.Default);
            if (adminSiteUri != null)
            {
                adminSite = adminWebApp.Sites[adminSiteUri.AbsoluteUri];
            }
            else
            {
                throw new InvalidProgramException("Unable to get Central Admin Site.");
            }

            return adminSite;
        }

    }
}
